package com.deyang.mycommon.ui.activity.study;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.deyang.mycommon.R;
import com.deyang.mycommon.ui.baseui.BaseActivity;
import com.deyang.mycommon.util.Logger;

import java.util.Random;

import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Completable;
import rx.CompletableSubscriber;
import rx.Observable;
import rx.Single;
import rx.SingleSubscriber;
import rx.Subscriber;
import rx.Subscription;

/**
 * @author wangdyang
 * @createDate 2018年7月28日09:25:06
 * @desc //RxJava的基础 使用
 */
public class RxJavaActivity extends BaseActivity {


    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_rx_java);
        ButterKnife.bind(this);
    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {

    }

    @OnClick({
            R.id.btn_send,
            R.id.btn_single,
            R.id.btn_completable,
            R.id.btn_operator,
    })
    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.btn_send:
                //被观察者   订阅/绑定   观察者        被观察者向观察者发送数据
                createSendIntegerObservable().subscribe(new Subscriber<Integer>() {
                    @Override
                    public void onCompleted() {
                        Logger.i(TAG, "我结束了  onCompleted");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Logger.i(TAG, "我出错了  onError：" + e.getMessage());
                    }

                    @Override
                    public void onNext(Integer integer) {
                        Logger.i(TAG, "正常接收数据  onNext：" + integer);
                    }
                });
                break;
            case R.id.btn_single:
                //single 创建 观察者 只发送一条数据         单个数据的生产者
                Single.create(new Single.OnSubscribe<Integer>() {
                    @Override
                    public void call(SingleSubscriber<? super Integer> singleSubscriber) {
                        if (!singleSubscriber.isUnsubscribed()) {
                            //singleSubscriber.onSuccess(1);
                            singleSubscriber.onError(new Throwable("哈哈哈"));
                        }
                    }
                }).subscribe(new SingleSubscriber<Integer>() {
                    @Override
                    public void onSuccess(Integer value) {
                        Logger.i(TAG, "Single onSuccess:" + value);
                    }

                    @Override
                    public void onError(Throwable error) {
                        Logger.i(TAG, "Single error:" + error.getMessage());
                    }
                });
                break;
            case R.id.btn_completable:
                //Completable是用来发送事件的 跟数据没有关系，  例如上传图片：只关心的是 上传是否成功
                //Completable.error(new Throwable("这里报个错"))
                Completable.complete()
                        .subscribe(new CompletableSubscriber() {
                            @Override
                            public void onCompleted() {
                                Logger.i(TAG, "上传成功  onCompleted");
                            }

                            @Override
                            public void onError(Throwable e) {
                                Logger.i(TAG, "上传失败  onError e:" + e.getMessage());
                            }

                            //可以用来取消订阅的方法
                            @Override
                            public void onSubscribe(Subscription d) {
                            }
                        });
                break;
            case R.id.btn_operator:
                //RxJava 操作符
                startActivity(new Intent(mApp, RxJavaOperatorActivity.class));
                break;
            case R.id.btn_ObservableConvert:
                //RxJava 转换操作符
                startActivity(new Intent(mApp, RxJavaObservableConvertActivity.class));
                break;
        }
    }

    /**
     * 创建一个 发送随机4个数的 观察者
     *
     * @return
     */
    public Observable<Integer> createSendIntegerObservable() {
        return Observable.create(new Observable.OnSubscribe<Integer>() {
            @Override
            public void call(Subscriber<? super Integer> subscriber) {
                //如果没有解除订阅
                if (!subscriber.isUnsubscribed()) {
                    for (int i = 0; i < 5; i++) {
                        int randomNum = new Random().nextInt(10);
                        //如果随机数 > 7 意外错误
                        if (randomNum > 7) {
                            subscriber.onError(new Throwable("randomNum > 7"));
                        } else {
                            subscriber.onNext(randomNum);
                        }

                        //最后一个结束
                        if (i == 4) {
                            subscriber.onCompleted();
                        }
                    }
                }
            }
        });
    }
}
